package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.impl.CategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 分类管理
 */
@RestController
@Slf4j
@Api(tags = "分类相关接口")
@RequestMapping("/admin/category")
public class CategoryController {


    @Autowired
    private CategoryServiceImpl categoryServiceImpl;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> getPageCategory(CategoryPageQueryDTO categoryPageQueryDTO) {

        log.info("进来了！");
        PageResult pageres=categoryServiceImpl.getPageCategory(categoryPageQueryDTO);

        return Result.success(pageres);
    }
    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {

        categoryServiceImpl.addCategory(categoryDTO);
        return Result.success();
    }
    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
  //      log.info("controller层的type：{}",categoryDTO.getType());
        categoryServiceImpl.updateCategory(categoryDTO);

        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<Category[]> listCategory(Integer type) {
        Category[]  res= categoryServiceImpl.listByCategory(type);
        return Result.success(res);
    }


    /**
     * 修改其状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改其状态")
    public Result startOrstop(@PathVariable Integer status,Long id) {
   //     categoryServiceImpl
        categoryServiceImpl.startOrstop(status,id);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteCategory(Long id) {
        categoryServiceImpl.deleteCategoryById(id);

        return Result.success();
    }

}
